package com.bnlr.lrparking.appui.unused;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bnlr.lrparking.R;
import com.bnlr.lrparking.appui.login.ForgetPassWordActivity;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.event.CloseLoginEvent;
import com.bnlr.lrparking.model.VerifyCodeM;
import com.bnlr.lrparking.util.IsPhoneFormatUtils;
import com.bnlr.lrparking.util.CommonUtil;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

/**
 * Created by hp on 2017/7/25.
 * 登录界面
 * 暂时未使用  2018-11-23
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener{

    private EditText edPhone,edPassword;
    private RelativeLayout relyLogin;
    private RelativeLayout relyForgetpaw;
    private RelativeLayout login_close;
    private TextView tvVerificaCode;
    private String phone,paw;
    private String appConfigPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        EventBus.getDefault().register(this);
        initView();
    }


    private void initView(){

        appConfigPhone=AppConfig.getInstance().getString("userName","");
        edPhone=(EditText)findViewById(R.id.ed_login_phone);
        edPhone.setText(appConfigPhone);
        edPhone.setSelection(appConfigPhone.length());//将光标移至文字末尾
        edPassword=(EditText)findViewById(R.id.ed_login_password);
        relyLogin=(RelativeLayout)findViewById(R.id.rely_login);
        relyForgetpaw=(RelativeLayout)findViewById(R.id.rely_login_forgetpaw);
        login_close=(RelativeLayout)findViewById(R.id.login_close);
        tvVerificaCode=(TextView)findViewById(R.id.tv_verifica_code);
        edPhone.addTextChangedListener(mTextWatcher);
        edPassword.addTextChangedListener(mTextWatcher);
        relyForgetpaw.setOnClickListener(this);
        login_close.setOnClickListener(this);
        tvVerificaCode.setOnClickListener(this);
        relyLogin.setOnClickListener(this);

    }


    private TextWatcher mTextWatcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            phone=edPhone.getText().toString();
            paw=edPassword.getText().toString();
            if (phone.length() ==11 && IsPhoneFormatUtils.isMobileNO(phone) && paw.length()>0){
                relyLogin.setBackgroundResource(R.drawable.seletor_login_btn);
                isButton(0);
            }else {
                relyLogin.setBackgroundResource(R.mipmap.login_disabled);
                isButton(1);
            }
        }
    };


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.rely_login_forgetpaw://忘记密码
                startActivity(ForgetPassWordActivity.class);
                break;
            case R.id.login_close://
                finish();
                break;
            case R.id.tv_verifica_code:
                finish();
                break;
        }
    }

    //按钮点击判断
    private void isButton(final int i){
        relyLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (i==0){
                    Login(1);
                }else if(i==1) {
                    Toast.makeText(LoginActivity.this, "请检查输入的账号密码", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    //验证码登录
    private void Login(int loginType) {
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP, LrConstant.POST);
            mRequest.add("action", "Login");
            mRequest.add("type", loginType);
            mRequest.add("phone", phone);
            mRequest.add("pwd", paw);
            // 添加到请求队列
            CallServer.getRequestInstance().add(LoginActivity.this, mRequest,
                    new CustomHttpListener<VerifyCodeM>(LoginActivity.this, true, VerifyCodeM.class) {
                        @Override
                        public void doWork(VerifyCodeM data, String code) {
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if ("1".equals(code)) {
                                    CommonUtil.showToask(LoginActivity.this,"登录成功");
                                    JSONObject object = obj.getJSONObject("data");
                                    AppConfig.getInstance().putBoolean("isLogin",true);
                                    AppConfig.getInstance().putString("uid",object.getString("uid"));
//                                    setAlias(AppConfig.getInstance().getString("uid",""));
                                    AppConfig.getInstance().putString("userName", object.getString("userName"));
                                    AppConfig.getInstance().putString("nickName", object.getString("nickName"));
                                    AppConfig.getInstance().putString("realName", object.getString("realName"));
                                    AppConfig.getInstance().putString("password", object.getString("password"));
                                    AppConfig.getInstance().putString("salt", object.getString("salt"));
                                    AppConfig.getInstance().putString("photo", object.getString("photo"));
                                    AppConfig.getInstance().putString("paypassword", object.getString("paypassword"));
                                    AppConfig.getInstance().putString("status", object.getString("status"));
                                    AppConfig.getInstance().putString("sharevalue", object.getString("sharevalue"));
                                    AppConfig.getInstance().putString("sharevalue_u", object.getString("sharevalue_u"));
                                    AppConfig.getInstance().putString("parktime", object.getString("parktime"));
                                    AppConfig.getInstance().putString("incomevalue", object.getString("incomevalue"));
                                    AppConfig.getInstance().putString("isRealNameAuth", object.getString("isRealNameAuth"));
                                    AppConfig.getInstance().putString("idcard", object.getString("idcard"));
                                    AppConfig.getInstance().putString("sex", object.getString("sex"));
                                    AppConfig.getInstance().putBoolean("isLogin",true);
                                    finish();
                                } else {
                                    CommonUtil.showToask(LoginActivity.this, obj.getString("msg"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCLose(CloseLoginEvent event) {
        finish();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
